// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import axios from 'axios'
import qs from 'qs'
import '../static/flexible'

import YDUI from 'vue-ydui';
import 'vue-ydui/dist/ydui.rem.css';
import './common/icon/iconfont.css'
import './common/icon/awesome-4.7.0/css/font-awesome.min.css'

import vueClipboard from 'vue-clipboard2'

import constant from './components/constant'
Vue.prototype.CONSTANT = constant

Vue.prototype.$httpAjax = axios
Vue.prototype.$axios = axios.create()
Vue.prototype.$qs = qs
Vue.use(YDUI);
Vue.use(vueClipboard)

Vue.config.productionTip = false

// http request 拦截器
// store.commit('scan','');

Vue.prototype.$axios.interceptors.request.use(
    config => {
        if (store.state.token) {
            // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers = {
                'x-auth-token': `${store.state.userid}_${store.state.token}`,
                'Content-Type': 'application/x-www-form-urlencoded'
            };
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    });

// http response 拦截器
Vue.prototype.$axios.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                if (error.response.config.url == constant.wapiBaseUrl + "member/memberDetail" || 
                    error.response.config.url == constant.wapiBaseUrl + "member/statistics" || 
                    error.response.config.url == constant.wapiBaseUrl + "activitySignIn/signInList" || 
                    error.response.config.url == constant.wapiBaseUrl + "healthCheck/userAppointList") {
                    return false;
                }
                // 返回 401 清除token信息并跳转到登录页面
                Vue.prototype.$dialog.toast({
                    mes: '请先登录！',
                    timeout: 1600
                });

                store.commit('tokenDel');
                router.push('/fastLogin');
            }
        }
        Promise.reject(error.response.data) // 返回接口返回的错误信息
    });

Vue.prototype.$httpAjax.interceptors.request.use(
    config => {
        if (store.state.token) {
            // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers = {
                'x-auth-token': `${store.state.userid}_${store.state.token}`
            };
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    });

// http response 拦截器
Vue.prototype.$httpAjax.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                // 返回 401 清除token信息并跳转到登录页面
                Vue.prototype.$dialog.toast({
                    mes: '请先登录！',
                    timeout: 1600
                });
                store.commit('tokenDel');
                router.replace('/fastLogin');
            }
        }
        return Promise.reject(error.response.data) // 返回接口返回的错误信息
    });

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: {
        App
    }
})